#!/usr/bin/env python
# -*- coding: utf8 -*-

from qtpy import QtCore, QtWidgets

class ThumbnailScaleableListView(QtWidgets.QListView):
    resized = QtCore.Signal()
    scaled = QtCore.Signal()
    def __init__(self, parent=None, load_step=50):
        super(ThumbnailScaleableListView, self).__init__(parent)
        # 视图缩放初始参数
        self.min_scale = 0.6
        self.current_scale = 1.0
        self.max_scale = 6.0
        self.item_spacing = 3
        # self.text_height = 30
        self._base_size = 170

        # 设置视图基本属性
        # self.setModel(ListViewLazyModel(self.__load_step))
        self.setViewMode(QtWidgets.QListView.IconMode)
        self.setMovement(QtWidgets.QListView.Static)
        self.setResizeMode(QtWidgets.QListView.Adjust)
        self.setFlow(QtWidgets.QListView.LeftToRight)
        self.setUniformItemSizes(True)
        # self.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.setSelectionRectVisible(True)
        self.setSpacing(12)

        # 设置初始尺寸
        # width = int(self.current_scale*128)
        # height = int(self.current_scale*128 + self.text_height)
        width = int(self.current_scale*self._base_size)
        height = int(self.current_scale*self._base_size)
        self.setIconSize(QtCore.QSize(width, height))
        # self.setGridSize(QtCore.QSize(width+self.item_spacing, height+self.item_spacing))
    def resizeEvent(self, event):
        super(ThumbnailScaleableListView, self).resizeEvent(event)
        self.resized.emit()

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_F:
            self.scale_location()
        else:
            super(ThumbnailScaleableListView, self).keyPressEvent(event)
            
    def wheelEvent(self, event):
        '''
        ctrl + 滚轮缩放视图
        '''
        if QtWidgets.QApplication.keyboardModifiers() == QtCore.Qt.ControlModifier:
            if (event.delta() > 0):
                self.zooming(0.2)
            else:
                self.zooming(-0.2)
            self.scaled.emit()
        else:
            super(ThumbnailScaleableListView, self).wheelEvent(event)

    def reset_scale(self, in_width):
        while int(self.current_scale*self._base_size) < in_width:
            self.zooming(0.2)

    def zooming(self, value):
        '''
        缩放视图
        '''
        to_scale = self.current_scale + value
        if to_scale <= self.max_scale and to_scale >= self.min_scale: 
            width = int(to_scale*self._base_size)
            # height = int(to_scale*self._base_size+35)
            height = int(to_scale*self._base_size)
            # if not self.is_show_thumbnail:
            #     height = self.no_thumbnail_height
            self.setIconSize(QtCore.QSize(width, height))
            # self.setGridSize(QtCore.QSize(width+self.item_spacing, height+self.item_spacing))
            self.scale_location()
            self.current_scale = to_scale

    def scale_location(self):
        '''
        缩放定位：缩放后定位缩放前的选择项
        '''
        index_list = self.selectionModel().selectedIndexes()
        if len(index_list) ==1:
            scroll_max = self.verticalScrollBar().maximum()
            if not scroll_max:
                return
            try:
                sel_rect = self.rectForIndex(index_list[0])
                widget_rect = self.rect()
                widget_contents = self.contentsSize()
                _value = float(sel_rect.y()-sel_rect.height()/2.0+widget_rect.height()/2)/(widget_contents.height()/float(scroll_max))
                if sel_rect.y() < widget_rect.height():
                    _value = 0
                if _value >= 0:
                    if _value > scroll_max:
                        _value = scroll_max
                    self.verticalScrollBar().setValue(int(_value))
            except:pass